/**
 * User: TIM
 * Date: 10/23/11
 * Time: 11:44 PM
 */

function initMenuTree(resourceJson, ctx, updateUrl) {
    updateUrl = ctx + updateUrl;
    var menuTree =
        $("#treeMenu").jstree({
                                  "json_data" : {
                                      "data" :resourceJson
                                  },
                                  "plugins" : [ "themes","json_data","ui","crrm","cookies","dnd","search","types","contextmenu" ] ,

                                  "types" : {
                                      "max_depth" : -2,
                                      "max_children" : -2,
                                      "valid_children" : [ "menu" ],
                                      "types" : {
                                          "access" : {
                                              "valid_children" : "none",
                                              "icon" : {
                                                  "image" : ctx + "/js/jstree/icon/url.png"
                                              }
                                          },
                                          "subMenu" : {
                                              "valid_children" : [ "access" ],
                                              "icon" : {
                                                  "image" :  ctx + "/js/jstree/icon/subMenu.png"
                                              }
                                          },
                                          "menu" : {
                                              "valid_children" : [ "subMenu" ],
                                              "icon" : {
                                                  "image" :  ctx + "/js/jstree/icon/menu.png"
                                              }
                                          } ,
                                          "root" : {
                                              "valid_children" : [ "menu" ],
                                              "icon" : {
                                                  "image" :  ctx + "/js/jstree/icon/menu.png"
                                              } ,
                                              "start_drag" : false,
                                              "move_node" : false,
                                              "delete_node" : false,
                                              "rename_node" : false,
                                              "remove_node" : false
                                          }
                                      }
                                  }
                              });
    menuTree.bind("create.jstree",
                  function (e, data) {
                      $.post(updateUrl, {
                                 "operation" : "create_node",
                                 "parentId" : data.rslt.parent.attr("id").replace("node_", ""),
                                 "position" : data.rslt.position,
                                 "title" : data.rslt.name,
                                 "type" : data.rslt.obj.attr("rel")
                             },
                             function (r) {
                                 if (r.status) {
                                     $(data.rslt.obj).attr("id", "node_" + r.id);
                                 }
                                 else {
                                     $.jstree.rollback(data.rlbk);
                                 }
                             }
                      );
                  });
    menuTree.bind("remove.jstree", function (e, data) {
        data.rslt.obj.each(function () {
            $.ajax({
                       async : false,
                       type: 'POST',
                       url: updateUrl,
                       data : {
                           "operation" : "remove_node",
                           "id" : this.id.replace("node_", "") ,
                           "revision" : data.rslt.obj.attr("revision")
                       },
                       success : function (r) {
                           if (!r.status) {
                               data.inst.refresh();
                           }
                       }
                   });
        });
    });
    menuTree.bind("rename.jstree",
                  function (e, data) {
                      $.post(
                          updateUrl,
                          {
                              "operation" : "rename_node",
                              "id" : data.rslt.obj.attr("id").replace("node_", ""),
                              "title" : data.rslt.new_name,
                              "revision" : data.rslt.obj.attr("revision")
                          },
                          function (r) {
                              if (!r.status) {
                                  $.jstree.rollback(data.rlbk);
                              }
                          }
                      );
                  })
    menuTree.bind("move_node.jstree", function (e, data) {
        data.rslt.o.each(function (i) {
            $.ajax({
                       async : false,
                       type: 'POST',
                       url: updateUrl,
                       data : {
                           "operation" : "move_node",
                           "id" : $(this).attr("id").replace("node_", ""),
                           "parentId" : data.rslt.cr == -1 ? 1 : data.rslt.np.attr("id").replace("node_", ""),
                           "position" : data.rslt.cp + i,
                           "title" : data.rslt.name,
                           "copy" : data.rslt.cy ? 1 : 0
                       },
                       success : function (r) {
                           if (!r.status) {
                               $.jstree.rollback(data.rlbk);
                           }
                           else {
                               $(data.rslt.oc).attr("id", "node_" + r.id);
                               if (data.rslt.cy && $(data.rslt.oc).children("UL").length) {
                                   data.inst.refresh(data.inst._get_parent(data.rslt.oc));
                               }
                           }
                       }
                   });
        });
    });
    return menuTree;
}
